package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定的构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class cls = Class.forName("reflect.Person");

        //获取无参构造器
        Constructor con1 = cls.getConstructor();
        Object obj1 = con1.newInstance();
        System.out.println(obj1);

        //获取有String参构造器
        Constructor con = cls.getConstructor(String.class);
        Object obj = con.newInstance("张三");
        System.out.println(obj);

        //获取有String和int参构造器
        Constructor con2 = cls.getConstructor(String.class,int.class);
        Object obj2 = con2.newInstance("李四",20);
        System.out.println(obj2);
    }
}
